<?php
/**
 * Smarty plugin
 * @category VaselinEngine
 * @package Smarty
 * @subpackage plugins
 */


/**
 * Smarty {calendar} function plugin
 *
 * Type:     function<br>
 * Name:     calendar<br>
 * Purpose:  print input field with calendar control. jQuery UI calendar is used
 * @author Konstantin Shamko <konstantin.shamko@gmail.com>
 * @param array parameters
 * @param Smarty
 * @return string|null
 */
function smarty_function_calendar($params, &$smarty) {

	unset ( $params ['label'], $params ['id'] );
	$params ['type'] = 'text';
	
	$input_attributes = array ('id', 'style', 'class', 'type', 'name', 'value', 'readonly', 'size', 'maxlength', 'alt', 'onfocus', 'onblur', 'onselect', 'onchange', 'onclick', 'ondblclick', 'onmousedown', 'onmouseup', 'onmouseover', 'onmousemove', 'onmouseout', 'onkeypress', 'onkeydown', 'onkeyup', 'disabled' );
	
	$input_params = $params;
	
	if($input_params['value']=='0000-00-00'){
		$input_params['value'] = NULL;
	}
	
	foreach ( $input_params as $key => $val ) {
		if (in_array ( ( string ) $key, $input_attributes ))
			$params [$key] = null; else
			$input_params [$key] = null;
	}

	echo '<input ';
	foreach ( $input_params as $key => $val ) {
		if ($val != null)
			echo $key . '="' . $val . '" ';
	}
	echo 'id="' . $input_params ['name'] . '_id"';
	echo ' onkeyup="this.value=\'\'"/>';

	$forbidden_dates = $params ['forbidden_dates'];
	
	$params ['inputField'] = $input_params ['name'] . '_id';
	$params ['button'] = $input_params ['name'] . '_button_id';
	
	$rnd = rand(10000,99999);
	?>
	
	<script type="text/javascript">
		function dateStatus<?=$rnd?>(date) {
			var forbidden = new Array();
			var cur_date = new Date();
	
			if(date.getTime()<cur_date.getTime()){
				return new Array(false);
			};

			<?php
			if ($forbidden_dates) {
				$i = 0;
				foreach ( $forbidden_dates as $date ) {
					echo "var min$i = new Date(" . $date ["start"] ["year"] . "," . $date ["start"] ["month"] . "-1," . $date ["start"] ["day"] . ");\n";
					echo "var max$i = new Date(" . $date ["end"] ["year"] . "," . $date ["end"] ["month"] . "-1," . $date ["end"] ["day"] . ");\n";
					echo "if( (date.getTime() >= min$i.getTime() && date.getTime() <= max$i.getTime()) ){\n";
					echo "  return new Array(false);\n";
					echo "}\n";
					$i ++;
				}
			}
			?>
			
			return new Array(true);};
	
			$(document).ready(function(){  
				$("#<?=$params ['inputField']?>").datepicker({     
					showOn: "both",    
					dateFormat: "yy-mm-dd",
					<?php
					if ($forbidden_dates) {
						echo 'beforeShowDay: dateStatus'.$rnd.',';
					}
					?>
		
					showStatus: true,
					yearRange: "-60:+10",
					buttonImage: "<?=$smarty->_tpl_vars ['theme']?>images/calendar.gif",
					buttonImageOnly: true 
				}).attr("readonly", "readonly");
			});	
	</script>
<?php	
}

function transformdate(&$date){
    $tmp = explode('-',$date);
    $date = $tmp[1].'-'.$tmp[2].'-'.$tmp[0];
}